package com.bootdo.appsystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.system.domain.DeptDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.ProductscategoryDO;
import com.bootdo.appsystem.service.ProductscategoryService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 商品类别
 * @author @fan
 * @email 1992lcg@163.com
 * @date 2018-07-12 11:45:26
 */
 
@Controller
@RequestMapping("/appsystem/productscategory")
public class ProductscategoryController {
	@Autowired
	private ProductscategoryService productscategoryService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:productscategory:productscategory")
	String Productscategory(){
	    return "appsystem/productscategory/productscategory";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:productscategory:productscategory")
	public List<ProductscategoryDO> list(){
		//查询列表数据
		Map<String, Object> query = new HashMap<>(16);
		List<ProductscategoryDO> productscategoryDOList = productscategoryService.list(query);
		return productscategoryDOList;
	}

    @GetMapping("/add/{procetid}")
    @RequiresPermissions("appsystem:productscategory:add")
    String add(@PathVariable("procetid") Integer procetid, Model model) {
        model.addAttribute("procetid", procetid);
        if (procetid == 0) {
            model.addAttribute("pName", "总分类");
        } else {
            model.addAttribute("pName", productscategoryService.get(procetid).getCategory());
        }
        return  "appsystem/productscategory/add";
    }



	@GetMapping("/edit/{procetid}")
	@RequiresPermissions("appsystem:productscategory:edit")
	String edit(@PathVariable("procetid") Integer procetid,Model model){
		ProductscategoryDO productscategory = productscategoryService.get(procetid);
		model.addAttribute("productscategory", productscategory);
	    return "appsystem/productscategory/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:productscategory:add")
	public R save( ProductscategoryDO productscategory){
		if(productscategoryService.save(productscategory)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:productscategory:edit")
	public R update( ProductscategoryDO productscategory){
		productscategoryService.update(productscategory);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:productscategory:remove")
	public R remove( Integer procetid){
		if(productscategoryService.remove(procetid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:productscategory:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] procetids){
		productscategoryService.batchRemove(procetids);
		return R.ok();
	}

    /**
     *【@author】  @fan
     *【方法解释】 商品分类三级联动
     *【@date】    2018/5/13 0013  下午 17:01
     */
    @ResponseBody
    @PostMapping("creatcategory")
    public ServerResponse<List<ProductscategoryController>> creatCategory(Integer topid){
        return productscategoryService.creatCategory(topid);
    }


	
}
